$(function () {
    //给type绑定点击事件
    $(".type").click(function () {
        var index = $(".type").index(this);
        var obj = $(".type").eq(index);
        $(".type").removeClass("checked");
        obj.addClass("checked");
    });
    //给color绑定点击事件
    $(".color").click(function () {
        var index = $(".color").index(this);
        var obj = $(".color").eq(index);
        $(".color").removeClass("checked");
        obj.addClass("checked");
    });
});

//商品数量++
function increase() {
    let quantity = $("#quantity").val();
    let stockStr = $("#stock").text();
    let stock = parseInt(stockStr);
    if (quantity < stock) {
        quantity++;
    }
    $("#quantity").val(quantity);
}

//商品数量--
function reduce() {
    let quantity = $("#quantity").val();
    if (quantity > 1) {
        quantity--;
    }
    $("#quantity").val(quantity);
}


//添加购物车
function addCart() {
    let productId = $("#productId").val();
    let price = $("#productPrice").val();
    let quantity = $("#quantity").val();
    let stockStr = $("#stock").text();
    let stock = parseInt(stockStr);
    if (stock < quantity) {
        alert("库存不足,库存为：" + stock);
    } else {
        window.location.href = "/cart/add/" + productId + "/" + price + "/" + quantity;
    }

}

function addOrders() {
    let productId = $("#productId").val();
    let integrals = $("#integrals").val();
    let totalIntegrals = $("#totalIntegrals").val();
    let quantity = $("#quantity").val();
    let stockStr = $("#stock").text();
    let stock = parseInt(stockStr);
    if (stock < quantity) {
        alert("库存不足,库存为：" + stock);
    } else if ((integrals * quantity) > totalIntegrals) {
        alert("积分不足，积分为：" + totalIntegrals + ",所需积分为：" + integrals * quantity);
    } else {
        window.location.href = "/orders/settlementIntegralPage/" + productId + "/" + quantity;
    }

}

